<template>
	<view class="wraps">
		<homeCom :hasUrl="hasUrl" @changeFag="changeFag" :zidong="zidong" :flag="flag" ref="home" @handleAuth="getCode" @changeTab="change"
			v-if="current == 0" />
		<template v-if="vuex_token8">
			<scan v-if="current == 1" />
			<mine @send="change" v-else-if="current == 2" />
		</template>
		<u-tabbar class="tabs" icon-size="48" :border-top="false" :hide-tab-bar="false" active-color="#FFFFFF" height="100"
		bg-color="linear-gradient(180deg, #FF5F63 0%, #DA2F31 100%)"
			inactive-color="#FFA0A2" @change="change"  v-model="current" :list="list">
		</u-tabbar>
	</view>
</template>

<script>
	import homeCom from "../home/index.vue"
	import scan from "../scan/index.vue"
	import mine from "../mine/index.vue"

	export default {
		components: {
			homeCom,
			mine,
			scan
		},
		data() {
			return {
				hasUrl:'',
				APPID:'',
				domain: '',
				info: null,
				flag: 1,
				zidong:false,
				code: '',
				shareCode: '',
				pageShow: '已通过',
				list: [{
						iconPath: "/static/you-magic/images/1.png",
						selectedIconPath: "/static/you-magic/images/1.png",
						text: '首页',
						customIcon: false,
					},
					{
						iconPath: "/static/you-magic/images/2.png",
						selectedIconPath: "/static/you-magic/images/2.png",
						text: '我要赚钱',
						midButton: true,
						customIcon: false,
					},
					{
						iconPath: "/static/you-magic/images/3.png",
						selectedIconPath: "/static/you-magic/images/3.png",
						text: '个人中心',
						customIcon: false,
					}
				],
				current: 0
			}
		},
		onLoad(e) {
			
			this.hasUrl = e?.url
			
			// if(this.is_weixn() && e.url){ //为了测试
			if(!this.is_weixn() && e.url){
				window.location.href = e.url
			}
			
			
			if(JSON.stringify(e) != "{}"){
				console.log('我的头上头参数@@@@');
				if(e?.tab){
					
					console.log('我走的是这里JSON.stringify(e) != "{}"');
					this.current = (e?.tab)
					if(this.current==0){
						this.Fnuseradd1()
					}
					console.log('我在这里调用了Fnuseradd1')
					
				}else{
					console.log('我走的是else~~~');
					if (this.vuex_token8) {
						console.log(this.vuex_token8, '我有token');
						this.Fnuseradd()
					} else {
						console.log(this.vuex_token8, '我么有openid');
						console.log('FnsetHasLogin---w我这里掉了缓存方法');
					
						this.FnsetHasLogin(true)
						this.flag = 4
					}
				}
					
			}else{
				console.log('我的头上头没有参数哦-=============@@@@');
				if (this.vuex_token8) {
					console.log(this.vuex_token8, '我有token');
					this.Fnuseradd()
				} else {
					console.log(this.vuex_token8, '我么有openid');
					this.flag = 4
				}
			}
			
				 this.addpv()
			
		},
		methods: {
			 is_weixn(){  
			    var ua = navigator.userAgent.toLowerCase();  
			    if(ua.match(/MicroMessenger/i)=="micromessenger") {  
			        return true;  
			    } else {  
			        return false;  
			    }  
			},

			changeFag(e) {
				this.flag = 2
			},

			addpv() {
				this.$u.api
					.pvadd()
					.then((res) => {});
			},

			getdomain() {
				this.$u.api
					.domain()
					.then((res) => {
					
						console.log(res.data, 777777);
					});
			},
			Fnuseradd1() {
				
			
			
		
				this.$u.api
					.useradd({
						shareCode: this.vuex_shareCode8,
						nickName: this.vuex_nickname8,
						openid: this.vuex_openId8,
						avater: this.vuex_avatar8
					})
					.then((res) => {
						console.log(res, 'add接口返回的参数');
			
						// alert(JSON.stringify(res))
						// redPacketStatus  1是 2否
						let {
							redPacketAmt,
							promotionCode,
							redPacketStatus,
							availableRebateAmt,
							availableRedPacketAmt
						} = res.data
			
						this.$u.vuex("vuex_redPacketAmt8", redPacketAmt); //红包金额
			
						this.$u.vuex("vuex_availableRebateAmt8", availableRebateAmt); //佣金余额
						// this.$u.vuex("vuex_availableRebateAmt8", 288); //佣金余额
			
						this.$u.vuex("vuex_promotionCode8", promotionCode); //海报
			
						this.$u.vuex("vuex_availableRedPacketAmt8", availableRedPacketAmt    ); //红包余额
						// this.$u.vuex("vuex_availableRedPacketAmt8", 0   ); //红包余额
						// redPacketStatus=1
						this.flag = 2
						this.zidong = true
							// this.getinputFn()
						
						
						// if (redPacketStatus == 1) {
						// 	this.flag = 3
						// } else{
						// 	this.flag = 2
						// 	this.zidong = true
						// }
					});
			},

			Fnuseradd(mytype) {

				
				this.$u.api
					.useradd({
						shareCode: this.vuex_shareCode8,
						nickName: this.vuex_nickname8,
						openid: this.vuex_openId8,
						avater: this.vuex_avatar8
					})
					.then((res) => {
						console.log(res, 'add接口返回的参数');

						// alert(JSON.stringify(res))
						// redPacketStatus  1是 2否
						let {
							redPacketAmt,
							promotionCode,
							redPacketStatus,
							availableRebateAmt,
							availableRedPacketAmt
						} = res.data

						this.$u.vuex("vuex_redPacketAmt8", redPacketAmt); //红包金额

						this.$u.vuex("vuex_availableRebateAmt8", availableRebateAmt); //佣金余额
						// this.$u.vuex("vuex_availableRebateAmt8", 288); //佣金余额
						

						this.$u.vuex("vuex_promotionCode8", promotionCode); //海报

						this.$u.vuex("vuex_availableRedPacketAmt8", availableRedPacketAmt  ); //红包余额
						
						
						if(mytype){
							console.log('我是第一次登陆成功时候掉的');
							// 我是第一次登陆成功时候掉的
							this.getinputFn()
						}
						
						// this.$u.vuex("vuex_availableRedPacketAmt8", 0   ); //红包余额
						// redPacketStatus=1
						if (redPacketStatus == 1) {
							this.flag = 3
						} else if (redPacketStatus == 2) {
							// this.flag = 1
							
							this.flag = 2
						}
					});
			},

			getCode() {

				// this.code = this.getUrlCode().code; // 截取code



				console.log(location.href, 'location.href~~~~~~~~~~~~~~~~~');


				this.$u.vuex("vuex_shareCode8", this.getUrlCode().shareCode || '');
				console.log('我走进了 getCode 方法');
				this.code = this.getUrlCode().openId; // 截取code
				// 如果没有code，则去请求
				if (this.code == null || this.code === "" || this.code == undefined) {


					console.log('vuex_token8 没拿到值');

					this.$u.api
						.domain()
						.then((res) => {

							const callbackURL = res.data.domain + '?shareCode=' + this.vuex_shareCode8 || ''
							
							window.location.href =
								`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${ res.data.appId}&redirect_uri=${callbackURL}&response_type=code&scope=snsapi_userinfo&forcePopup=true&forceSnapShot=false#wechat_redirect`

						});

				} else {

					this.FnsetHasLogin()


				}
			},



			FnsetHasLogin(mytype) {
				// here
			
				this.$u.vuex("vuex_openId8", this.getUrlCode().openId);
				this.$u.vuex("vuex_token8", this.getUrlCode().openId);

				this.$u.api
					.userinfo()
					.then((res) => {
						let {
							avater,
							nickName,
							openId
						} = res.data
						this.$u.vuex("vuex_avatar8", avater);
						this.$u.vuex("vuex_nickname8", nickName);
						this.$u.vuex("vuex_openId8", openId);
						
						
						this.Fnuseradd(mytype)
						
						
						
					});



				// this.Fnuseradd(mytype)




			},
			
			getinputFn(){
				this.$u.api
					.getinput({
						avater:this.vuex_avatar8,
						nickName:this.vuex_nickname8,
						userid: this.vuex_token8,
						amt: this.vuex_redPacketAmt8
					})
					.then((res) => {
						
					});
			},
			getUrlCode() {
				// 截取url中的code方法
				var url = location.href; //获取打开的公众号的路径
				let winUrl = url;
				var theRequest = new Object();
				if (url.indexOf("?") != -1) {
					var str = url.substr(url.indexOf("?") + 1);
					var strs = str.split("&");
					for (var i = 0; i < strs.length; i++) {
						var items = strs[i].split("=");
						theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
					}
				}
				return theRequest;
			},
			change(i) {
				
				console.log(i,66666666);
				if (this.vuex_token8) {
					this.current = i
				} else {
					if ([1, 2].includes(i)) {
						this.getCode()

					}
				}
			}
		},
	}
</script>


<style scoped lang="scss">
	/deep/ .u-tabbar__content {
		padding-bottom: env(safe-area-inset-bottom) !important; // 底部安全区
		background: linear-gradient(180deg, #FF5F63 0%, #DA2F31 100%) !important;
	}
	.tabs /deep/ uni-view{
		border-radius: 15px 15px 0  0 !important;
	}
	/deep/ .u-icon__img{
		width: 70rpx !important;
		height: 70rpx !important;
	}
	

	/deep/ .u-tabbar__content__item__text {
		margin-bottom: 3px !important;
	}
</style>
